<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\util\File;
use app\appv110\model\Yuyue;
use app\appv110\model\StaffCtm;
use app\appv110\model\StoreCtm;
use app\appv110\model\StaffBindStore;
use app\util\Callout;
use app\appv110\model\Staff;

class TestController extends CommonController {
    /*
     * 扫码领取红包兑换特权
     * 日期 2019-09-29
     */

    public function demo() {
        return $relust = Utils::curlPost('http://app.dwkkm.com/kkmaitest/public/admin/Index/index/', '');
    }

    /*
     * 美容师接单量
     * 日期 2020-01-02
     */

    public function ctmstaffNum() {
        $list = Yuyue::alias('y')
                ->join('staff s', 's.id = y.staff_id', 'left')
                ->where('y.status', 'in', '4,7,8')
                ->field('count(y.id) num,s.staff_name,s.add_time')
                ->group('y.staff_id')
                ->select();
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=3 style='border:1px solid #999999;'>美容师接单数 </td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>美容师</td>"
        . "<td style='border:1px solid #999999;'>接单数</td>"
        . "<td style='border:1px solid #999999;'>注册时间</td>"
        . "</tr>";

        foreach ($list as $k => $v) {
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['staff_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['num'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['add_time'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /* 将美容师的客户复制给店铺 */

    public function staffCtmToStoreCtm() {
        $list = StaffCtm::select();
        foreach ($list as $k => $v) {
            $row = StaffBindStore::where('staff_id', $v['staff_id'])->find();
            if ($row) {
                $storeCtmRow = StoreCtm::where('ctm_id', $v['ctm_id'])->where('store_id', $row['store_id'])->find();
                if (empty($storeCtmRow)) {
                    $data = [
                        'type' => 6,
                        'store_id' => $row['store_id'],
                        'staff_id' => $v['staff_id'],
                        'ctm_id' => $v['ctm_id'],
                        'add_time' => $v['add_time'],
                    ];
                    StoreCtm::insert($data);
                }
            }
        }
        echo '复制成功';
    }

    /* 测试小号 */

    public function midNum() {
        $midNum = '17081082707';
        $expiration = 180;
        $phone = 15733262675;
        $caller = 18032376697;
        $Callout = new Callout();
        $result = $Callout->midNum($midNum, $phone, $expiration);
        //$result = $Callout->midNumAXB($midNum, $phone, $caller, $expiration);

        $result = json_decode($result, true);
        return Utils::ajaxReturn(0, '666', $result);
    }

    /* 查询购买9.9的情况 */

    public function aaa() {
        //购买了9.9并且使用了的
        $num = Yuyue::alias('y')
                ->join('order o', 'o.id = y.order_id', 'left')
                ->where('o.first_exemption', 2)
                ->where('o.status', 2)
                ->where('y.status', 'in', '4,7,8')
                ->column('o.ctm_id');
        //使用了9.9后又购买了其他项目的
        $num1 = \app\appv110\model\Order::where('status', 2)
                ->where('type', 'in', '1,2,5')
                ->where('first_exemption', '<>', 2)
                ->where('ctm_id', 'in', $num)
                ->count();
        print_r($num1);
    }

    /* 查询99特权数量及购买商品信息 */

    public function getSpecialCtm() {
        $ctmNumList = \app\appv110\model\Custom::where('vip_status', 2)->column('id');
        $ctmNum = count($ctmNumList);
        $buyGoodsNum = Db::table('pay_order')->alias('p')
                ->join('custom c', 'c.id = p.ctm_id', 'left')
                ->where('c.vip_status', 2)
                ->where('p.status', '>', 1)
                ->group('p.ctm_id')
                ->count();
        $buyGoodsMoney = Db::table('pay_order')->alias('p')
                ->join('custom c', 'c.id = p.ctm_id', 'left')
                ->where('c.vip_status', 2)
                ->where('p.status', '>', 1)
                ->sum('p.pay_money');
        $buyGoodscount = Db::table('pay_order')->alias('p')
                ->join('custom c', 'c.id = p.ctm_id', 'left')
                ->where('c.vip_status', 2)
                ->where('p.status', '>', 1)
                ->column('order_id');
        $ids = Db::table('pay_order_detail')->where('parent_order_id', 'in', $buyGoodscount)->column('id');
        $num = Db::table('pay_order_goods')->where('pay_order_id', 'in', $ids)->count();
        $refundnum = Db::table('pay_order_goods')->where('pay_order_id', 'in', $ids)->where('is_refund', 'in', '1,2')->count();
        $agin = \app\appv110\model\Order::where('ctm_id', 'in', $ctmNumList)
                ->where('type', 'in', '1,2,5')
                ->where('first_exemption', '<>', 2)
                ->where('status', 2)
                ->group('ctm_id')
                ->count();

        echo "特权用户：" . $ctmNum;
        echo "<br>购买过商品的特权用户：" . $buyGoodsNum;
        echo "<br>购买商品总金额：" . $buyGoodsMoney / 100;
        echo "<br>购买商品总笔数：" . $num;
        echo "<br>购买商品总退款笔数：" . $refundnum;
        echo "<br>特权用户再次购买项目的用户数：" . $agin;
    }

    /* 跑美容师02月的全勤分 */

    public function staffscore() {
        $BeginDate = '2021.02.01';
        $endDate = '2021.02.28 23:59:59';
        $stafflist = Db::name('staff_sign')
                ->field('staff_id')
                ->where('add_time', 'between', [$BeginDate, $endDate])
                ->group('staff_id')
                ->select();
        //print_r($stafflist);die;
        foreach ($stafflist as $k => $v) {
            $staff_id = $v['staff_id'];
            //查美容师所在的团队配置的满勤天数是多少
            $team_id = Db::name('staff_team_details')->where('staff_id', $staff_id)->where('status', 'in', '1,2')->value('team_id');
            if ($team_id) {
                $base_days = Db::name('staff_team')->where('id', $team_id)->value('base_days');
                //查美容师本月打卡次数
                //$BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
                //$endDate = date('Y-m-d', strtotime("$BeginDate +1 month"));
                $shiji_days = Db::name('staff_sign')->where('staff_id', $staff_id)
                        ->where('add_time', 'between', [$BeginDate, $endDate])
                        //->field('count(id) num')
                        //->group("DATE_FORMAT(FROM_UNIXTIME(add_time),'%m-%d-%Y')")
                        ->group("date_format(add_time,'%m-%d-%Y')")
                        ->count();
                //$shiji_days = $shiji_days[0]['num'];
                //print_r($BeginDate.'==='.$endDate);die;
                //print_r($shiji_days.'--');
                if ($shiji_days >= $base_days) {//打卡 达到满勤天数
                    $row = Db::name('staff_assess_score')
                            ->where('staff_id', $staff_id)
                            ->where('score_type', 2)
                            ->where('add_time', 'between', [$BeginDate, $endDate])
                            ->find();
                    if (empty($row)) {//本月还没有记录满勤分呢
                        echo '666---';
                        $data['staff_id'] = $staff_id;
                        $data['type'] = 1;
                        $data['score_type'] = 2;
                        $data['score'] = 20;
                        $data['remarks'] = '当月出勤满' . $base_days . '天，基础分加20分';
                        $data['add_time'] = '2021.02.28 00:20:00';
                        //Db::name('staff_assess_score')->insert($data);
                    }
                }
            }
        }
    }

    /*
     * 吾悦广场店 待预约订单
     * 日期 2021-04-13
     */

    public function storeNoYuyue() {
        $list = Yuyue::alias('y')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->join('item i', 'i.id = y.item_id', 'left')
                ->where('y.status', 'in', '1,5,9,10,12')
                ->where('y.store_id', 75)
                ->field('y.id,y.add_time,c.custom_name,c.custom_phone,i.item_name')
                ->select();
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=5 style='border:1px solid #999999;'>吾悦广场店 待预约订单 </td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>用户姓名</td>"
        . "<td style='border:1px solid #999999;'>手机号</td>"
        . "<td style='border:1px solid #999999;'>项目名称</td>"
        . "<td style='border:1px solid #999999;'>时间</td>"
        . "</tr>";

        foreach ($list as $k => $v) {
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['custom_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['custom_phone'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['item_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['add_time'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 导出店铺的空间信息
     * 日期 2021-04-27
     */

    public function storeRoom() {
        $list = Db::name('store_room')
                ->alias('sr')
                ->join('store s', 's.id = sr.store_id', 'left')
                ->join('prov_city_area p', 'p.id = s.city', 'left')
                ->where('sr.active', 1)
                ->where('sr.is_delete', 1)
                ->field('sr.id,sr.room_name,sr.add_time,sr.smart_status,sr.robot_no,s.store_name,p.name')
                ->order('sr.store_id asc s.city asc')
                ->select();

        foreach ($list as $k => $v) {
            $bednum = Db::name('store_room_bed')
                    ->where('room_id', $v['id'])
                    ->where('active', 1)
                    ->where('is_delete', 1)
                    ->count();
            $list[$k]['bednum'] = $bednum;
            if ($v['smart_status'] == 1) {
                $list[$k]['type'] = '智能';
            } else {
                $list[$k]['type'] = '多功能';
            }
        }
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=8 style='border:1px solid #999999;'>DAVID KKM店铺空间信息 </td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>店铺名称</td>"
        . "<td style='border:1px solid #999999;'>所在城市</td>"
        . "<td style='border:1px solid #999999;'>房间名称</td>"
        . "<td style='border:1px solid #999999;'>房间类型</td>"
        . "<td style='border:1px solid #999999;'>机器码</td>"
        . "<td style='border:1px solid #999999;'>床位数</td>"
        . "<td style='border:1px solid #999999;'>时间</td>"
        . "</tr>";

        foreach ($list as $k => $v) {
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['store_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['room_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['type'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['robot_no'] . "</td>";
            echo "<td style='border:1px solid #999999;'>"
            . $v['bednum'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['add_time'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 导出体验卡的订单
     * 日期 2021-04-29
     */

    public function superCardOrderList() {
        $custom_phone = input('param.custom_phone'); //用户手机号
        $status = input('param.status'); //1无效订单 2有效订单
        if ($custom_phone) {
            $where['c.custom_phone'] = $custom_phone;
        }
        if ($status == 1) {
            $where['sco.status'] = ['<>', 2];
        } elseif ($status == 2) {
            $where['sco.status'] = ['in','2,3,4,5'];
        }
        $where = isset($where) ? $where : [];
        $list = Db::name('super_card_order')
                ->alias('sco')
                ->join('custom c', 'c.id = sco.ctm_id', 'left')
                ->where($where)
                ->field('sco.*,c.custom_phone,c.custom_name,c.longitude,c.latitude')
                ->order('sco.add_time desc')
                ->select();

        foreach ($list as $k => $v) {
            $status = $v['status'];
            $enjoy_type = $v['enjoy_type'];
            if ($status == 0) {//	订单状态 订单状态 0已取消 1待支付 2已支付=待预约 3待服务 4已完成 5已退款	
                $statusName = '已取消';
            } elseif ($status == 1) {
                $statusName = '待支付';
            } elseif ($status == 2) {
                $statusName = '已支付';
            } elseif ($status == 3) {
                $statusName = '待服务';
            }elseif ($status == 4) {
                $statusName = '已完成';
            }elseif ($status == 5) {
                $statusName = '已退款';
            }
            $list[$k]['status'] = $statusName;
            if ($enjoy_type == 1) {//1.美容师 2.店铺 3.用户
                $enjoyName = '美容师';
                $enjoy_idName = Staff::where('id', $v['enjoy_id'])->value('staff_name');
            } elseif ($enjoy_type == 2) {
                $enjoyName = '店铺';
                $enjoy_idName = \app\appv110\model\Store::where('id', $v['enjoy_id'])->value('store_name');
            } elseif ($enjoy_type == 3) {
                $enjoyName = '用户';
                $enjoy_idName = \app\appv110\model\Custom::where('id', $v['enjoy_id'])->value('custom_name');
            } elseif ($enjoy_type == 0) {
                $enjoyName = '平台';
                $enjoy_idName = '自主下单';
            }
            $list[$k]['enjoy_type'] = $enjoyName;
            $list[$k]['enjoy_id'] = $enjoy_idName;
            //查询用户的地址
            if($v['latitude'] && $v['longitude']){
                $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $v['latitude'] . "," . $v['longitude'] . "&key=ZJOBZ-PVNKD-MMV4P-HLV6T-X3BM3-Q3FBD";
                $relust = Utils::curlGet($url);
                $relust = json_decode($relust, true);
                $list[$k]['address'] = $relust['result']['address'];
                $list[$k]['city'] = $relust['result']['ad_info']['city'];
            }  else {
                $list[$k]['address'] = '未知';
                $list[$k]['city'] = '未知';
            }
            
        }
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=16 style='border:1px solid #999999;'>DAVID KKM店铺空间信息 </td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>用户姓名</td>"
        . "<td style='border:1px solid #999999;'>用户手机号</td>"
        . "<td style='border:1px solid #999999;'>支付金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付状态</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>分享类型</td>"
        . "<td style='border:1px solid #999999;'>分享人</td>"
        . "<td style='border:1px solid #999999;'>下单城市</td>"
        . "<td style='border:1px solid #999999;'>下单地址</td>"
        . "<td style='border:1px solid #999999;'>下单时间</td>"
        . "<td style='border:1px solid #999999;'>子项目一</td>"
        . "<td style='border:1px solid #999999;'>子项目二</td>"
        . "<td style='border:1px solid #999999;'>子项目三</td>"
        . "<td style='border:1px solid #999999;'>子项目四</td>"
        . "</tr>";

        foreach ($list as $k => $v) {
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['custom_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['custom_phone'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_money'] / 100 . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['status'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['order_no'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['enjoy_type'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['enjoy_id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['city'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['address'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['add_time'] . "</td>";
            $yuyuelist = Db::name('super_card_yuyue')
                    ->alias('y')
                    ->join('super_card_item i','i.id = y.item_id','left')
                    ->join('store s','s.id = y.store_id','left')
                    ->where('y.order_id',$v['id'])
                    ->order('y.item_id asc')
                    ->field('i.item_name,y.status,y.ser_time,s.store_name')
                    ->select();
            foreach ($yuyuelist as $key => $val){
                $status = $val['status'];//1待预约2待服务3已完成4活动结束5预约超时6取消预约7预约失败	
                if($status == 1){
                    $a = '待预约';
                }elseif ($status == 2) {
                    $a = '待服务';
                }elseif ($status == 3) {
                    $a = '已完成';
                }elseif ($status == 4) {
                    $a = '活动结束';
                }elseif ($status == 5) {
                    $a = '预约超时';
                }elseif ($status == 6) {
                    $a = '取消预约';
                }elseif ($status == 7) {
                    $a = '预约失败';
                }
                
                echo "<td style='border:1px solid #999999;'>" . $val['item_name'].'--'.$a.'('.$val['store_name'].$val['ser_time'].')'."</td>";
            }
                    
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 小店订单
     * 日期 2021-05-14
     */

    public function goodsOrderList() {
        $list = Db::name('vender_flow')
                ->where('pro_type', 'in', '7,9')
                ->where('type', 1)
                ->order('add_time desc')
                ->select();
        foreach ($list as $k => $v) {
            $brand_id = $v['brand_id'];
            $order_id = $v['order_id'];
            $pro_type = $v['pro_type'];
            $brand_name = Db::table('brand_info')->where('id', $brand_id)->value('name');
            $companyId = Db::connect('mysql://root:eDKmpHzrfkcKrHavT5NLuQ@127.0.0.1:3306/mbcnew#utf8')
                    ->table('brand_info')
                    ->where('name', $brand_name)
                    ->value('companyId');
            $companyRow = Db::connect('mysql://root:eDKmpHzrfkcKrHavT5NLuQ@127.0.0.1:3306/mbcnew#utf8')
                    ->table('company_info')
                    ->where('id', $companyId)
                    ->find();
            $list[$k]['companyName'] = $companyRow['name'];
            $list[$k]['companyTel'] = $companyRow['dutyPersonPhone'];
            $list[$k]['alipay'] = $companyRow['alipay'];
            $list[$k]['alipay_name'] = $companyRow['alipay_name'];
            $list[$k]['bank_name'] = $companyRow['bank_name'];
            $list[$k]['bank_address'] = $companyRow['bank_address'];

            $orderRow = Db::table('pay_order_detail')
                    ->where('id', $order_id)
                    ->field('pay_time,pay_money')
                    ->find();
            $list[$k]['pay_time'] = $orderRow['pay_time'];
            $list[$k]['pay_money'] = $orderRow['pay_money'];
            //kkm利润
            $kkm = Db::name('kkm_flow')
                    ->where('order_id', $order_id)
                    ->where('pro_type', $pro_type)
                    ->value('money');
            $list[$k]['kkm_money'] = $kkm / 100;
            //查是否已经提现
            $tixian = Db::name('vender_tixian')
                    ->where('find_in_set(:id,order_ids)', ['id' => $order_id])
                    ->find();
            if ($tixian) {
                $list[$k]['kkm_status'] = '已生成提现';
                $list[$k]['kkm_time'] = $tixian['utime'];
            } else {
                $list[$k]['kkm_status'] = '未提现';
                $list[$k]['kkm_time'] = '###';
            }
        }



        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=18 style='border:1px solid #999999;'>小店订单 </td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>厂商名称</td>"
        . "<td style='border:1px solid #999999;'>手机号</td>"
        . "<td style='border:1px solid #999999;'>付款时间</td>"
        . "<td style='border:1px solid #999999;'>付款方式</td>"
        . "<td style='border:1px solid #999999;'>订单金额</td>"
        . "<td style='border:1px solid #999999;'>订单手续费</td>"
        . "<td style='border:1px solid #999999;'>实际到账金额</td>"
        . "<td style='border:1px solid #999999;'>提现金额</td>"
        . "<td style='border:1px solid #999999;'>手续费</td>"
        . "<td style='border:1px solid #999999;'>实际到账金额</td>"
        . "<td style='border:1px solid #999999;'>克克美利润</td>"
        . "<td style='border:1px solid #999999;'>姓名</td>"
        . "<td style='border:1px solid #999999;'>结算账号</td>"
        . "<td style='border:1px solid #999999;'>银行名称</td>"
        . "<td style='border:1px solid #999999;'>开户行</td>"
        . "<td style='border:1px solid #999999;'>提现状态</td>"
        . "<td style='border:1px solid #999999;'>提现日期</td>"
        . "</tr>";

        foreach ($list as $k => $v) {
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['companyName'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['companyTel'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . '小程序' . "</td>";
            $pay_money = $v['pay_money'] / 100;
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            $shouxufei = $v['pay_money'] / 100 * 0.006;
            $shijidaozhang = $v['pay_money'] / 100 * (1 - 0.006);
            echo "<td style='border:1px solid #999999;'>" . $shouxufei . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $shijidaozhang . "</td>";
            $money = $v['money'] / 100;
            echo "<td style='border:1px solid #999999;'>" . $money . "</td>";
            echo "<td style='border:1px solid #999999;'></td>";
            echo "<td style='border:1px solid #999999;'></td>";
            echo "<td style='border:1px solid #999999;'>" . $v['kkm_money'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['alipay_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['alipay'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['bank_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['bank_address'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['kkm_status'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['kkm_time'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 店铺流水
     * 日期 2021-05-18
     */

    public function storeFlowList() {
        $storeList = Db::name('store')
                ->field('id,store_name,money')
                ->where('id', 'not in', '1,46')
                ->select();
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=18 style='border:1px solid #999999;'>无人店店铺流水 </td></tr>";

        foreach ($storeList as $k => $v) {
            echo "<tr style=color:red>"
            . "<td style='border:1px solid #999999;'>编号</td>"
            . "<td style='border:1px solid #999999;'>店铺名称</td>"
            . "<td style='border:1px solid #999999;'>店铺总营收</td>"
            . "<td style='border:1px solid #999999;'>已提现金额</td>"
            . "<td style='border:1px solid #999999;'>店铺余额</td>"
            . "</tr>";
            $store_id = $v['id'];
            $money = $v['money'] / 100;
            //店铺总营收
            $allMoney = Db::name('store_flow')
                    ->where('store_id', $store_id)
                    ->where('type', 1)
                    ->sum('money');
            $allMoney = $allMoney / 100;
            //提现总金额
            $tixianMoney = Db::name('store_tixian')
                    ->where('store_id', $store_id)
                    ->where('status', '<>', 3)
                    ->sum('money');
            $tixianshouxufei = Db::name('store_tixian')
                    ->where('store_id', $store_id)
                    ->where('status', '<>', 3)
                    ->sum('poundage');
            $tixian = ($tixianMoney + $tixianshouxufei) / 100;
            echo "<tr style=color:red>"
            . "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $v['store_name'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $allMoney . "</td>"
            . "<td style='border:1px solid #999999;'>" . $tixian . "</td>"
            . "<td style='border:1px solid #999999;'>" . $money . "</td>"
            . "</tr>";

            echo "<tr>"
            . "<td style='border:1px solid #999999;'>编号</td>"
            . "<td style='border:1px solid #999999;'>项目名</td>"
            . "<td style='border:1px solid #999999;'>订单编号</td>"
            . "<td style='border:1px solid #999999;'>实付金额</td>"
            . "<td style='border:1px solid #999999;'>付款时间</td>"
            . "<td style='border:1px solid #999999;'>支付方式</td>"
            . "<td style='border:1px solid #999999;'>用户姓名</td>"
            . "<td style='border:1px solid #999999;'>用户手机号</td>"
            . "<td style='border:1px solid #999999;'>入账金额</td>"
            . "</tr>";
            $list = Db::name('store_flow')
                    ->where('store_id', $store_id)
                    ->where('type', 1)
                    ->order('add_time desc')
                    ->select();
            foreach ($list as $key => $val) {
                //订单信息
                if ($val['pro_type'] == 1 || $val['pro_type'] == 2 || $val['pro_type'] == 5) {
                    $yuyueid = $val['order_id'];
                    $order_id = Db::name('yuyue')->where('id', $yuyueid)->value('order_id');
                    $orderRow = Db::name('order')
                            ->alias('o')
                            ->join('custom c', 'c.id = o.ctm_id', 'left')
                            ->where('o.id', $order_id)
                            ->field('o.ord_num,o.pay_time,o.pay_money,o.ordtype,c.custom_name,c.custom_phone')
                            ->find();
                } elseif ($val['pro_type'] == 4 || $val['pro_type'] == 9 || $val['pro_type'] == 15) {
                    $order_id = $val['order_id'];
                    $orderRow = Db::name('order')
                            ->alias('o')
                            ->join('custom c', 'c.id = o.ctm_id', 'left')
                            ->where('o.id', $order_id)
                            ->field('o.ord_num,o.pay_time,o.pay_money,o.ordtype,c.custom_name,c.custom_phone')
                            ->find();
                } elseif ($val['pro_type'] == 7 || $val['pro_type'] == 11) {
                    $pay_order_detail = $val['order_id'];
                    $orderRow = Db::table('pay_order_detail')
                            ->alias('o')
                            ->join('custom c', 'c.id = o.ctm_id', 'left')
                            ->where('o.id', $pay_order_detail)
                            ->field('o.parent_order_id ord_num,o.pay_time,o.pay_money,c.custom_name,c.custom_phone')
                            ->find();
                    $orderRow['ordtype'] = 1;
                }
                $pay_money = $orderRow['pay_money'] / 100;
                if ($orderRow['ordtype'] == 1) {
                    $ordtype = '小程序';
                } elseif ($orderRow['ordtype'] == 2) {
                    $ordtype = '余额';
                }
                $a = $val['money'] / 100;
                echo "<tr>"
                . "<td style='border:1px solid #999999;'>" . $val['id'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $val['item_name'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $orderRow['ord_num'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $pay_money . "</td>"
                . "<td style='border:1px solid #999999;'>" . $orderRow['pay_time'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $ordtype . "</td>"
                . "<td style='border:1px solid #999999;'>" . $orderRow['custom_name'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $orderRow['custom_phone'] . "</td>"
                . "<td style='border:1px solid #999999;'>" . $a . "</td>"
                . "</tr>";
            }
        }
        echo "</table>";
    }

    /*
     * 店铺结余汇总
     * 日期 2021-05-25
     */

    public function storeMoneyAll() {
        $storeList = Db::name('store')
                ->field('id,store_name,money')
                ->where('id', 'not in', '1,46')
                ->select();
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=5 style='border:1px solid #999999;'>店铺结余汇总</td></tr>";
        echo "<tr align='center'><td colspan=5 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>店铺名称</td>"
        . "<td style='border:1px solid #999999;'>店铺总营收</td>"
        . "<td style='border:1px solid #999999;'>已提现金额</td>"
        . "<td style='border:1px solid #999999;'>店铺余额</td>"
        . "</tr>";
        foreach ($storeList as $k => $v) {

            $store_id = $v['id'];
            $money = $v['money'] / 100;
            //店铺总营收
            $allMoney = Db::name('store_flow')
                    ->where('store_id', $store_id)
                    ->where('type', 1)
                    ->sum('money');
            $allMoney = $allMoney / 100;
            //提现总金额
            $tixianMoney = Db::name('store_tixian')
                    ->where('store_id', $store_id)
                    ->where('status', '<>', 3)
                    ->sum('money');
            $tixianshouxufei = Db::name('store_tixian')
                    ->where('store_id', $store_id)
                    ->where('status', '<>', 3)
                    ->sum('poundage');
            $tixian = ($tixianMoney + $tixianshouxufei) / 100;
            echo "<tr>"
            . "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $v['store_name'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $allMoney . "</td>"
            . "<td style='border:1px solid #999999;'>" . $tixian . "</td>"
            . "<td style='border:1px solid #999999;'>" . $money . "</td>"
            . "</tr>";
        }
        echo "</table>";
    }

    /*
     * 美容师结余汇总
     * 日期 2021-05-25
     */

    public function staffMoneyAll() {
        $storeList = Db::name('staff')
                ->field('id,staff_name,money,staff_on')
                ->select();
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=6 style='border:1px solid #999999;'>美容师结余汇总</td></tr>";
        echo "<tr align='center'><td colspan=6 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>美容师姓名</td>"
        . "<td style='border:1px solid #999999;'>是否在职</td>"
        . "<td style='border:1px solid #999999;'>美容师入账金额</td>"
        . "<td style='border:1px solid #999999;'>美容师累计提现</td>"
        . "<td style='border:1px solid #999999;'>美容师余额</td>"
        . "</tr>";
        foreach ($storeList as $k => $v) {

            $staff_id = $v['id'];
            $money = $v['money'] / 100;
            $staff_on = $v['staff_on'];
            if ($staff_on == 1) {
                $staff_on = '在职';
            } elseif ($staff_on == 2) {
                $staff_on = '离职';
            }
            //店铺总营收
            $allMoney = Db::name('staff_flow_new')
                    ->where('staff_id', $staff_id)
                    ->where('type', 1)
                    ->where('status', 2)
                    ->sum('money');
            $allMoney = $allMoney / 100;
            //提现总金额
            $tixianMoney = Db::name('staff_tixian')
                    ->where('staff_id', $staff_id)
                    ->where('status', '<>', 3)
                    ->sum('money');
            $tixianshouxufei = Db::name('staff_tixian')
                    ->where('staff_id', $staff_id)
                    ->where('status', '<>', 3)
                    ->sum('poundage');
            $tixian = ($tixianMoney + $tixianshouxufei) / 100;
            echo "<tr>"
            . "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $v['staff_name'] . "</td>"
            . "<td style='border:1px solid #999999;'>" . $staff_on . "</td>"
            . "<td style='border:1px solid #999999;'>" . $allMoney . "</td>"
            . "<td style='border:1px solid #999999;'>" . $tixian . "</td>"
            . "<td style='border:1px solid #999999;'>" . $money . "</td>"
            . "</tr>";
        }
        echo "</table>";
    }

    /*
     * 无人店~项目订单明细
     * 日期 2021-05-25
     */

    public function itemOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=12 style='border:1px solid #999999;'>项目订单明细</td></tr>";
        echo "<tr align='center'><td colspan=12 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>项目名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>零钱金额</td>"
        . "<td style='border:1px solid #999999;'>退款金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "<td style='border:1px solid #999999;'>分账状态</td>"
        . "<td style='border:1px solid #999999;'>店铺入账</td>"
        . "<td style='border:1px solid #999999;'>美容师入账</td>"
        . "<td style='border:1px solid #999999;'>平台入账</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));
        
//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';

        $where['o.status'] = 2;
        $where['o.type'] = ['in', '1,2,5'];
        $where['o.pay_time'] = ['between', [$startTime, $endTime]];
//        $where['sfc.pro_type'] = ['in','1,2,5'];
        $orderList = Db::name('yuyue')
                ->alias('y')
                ->join('order o', 'o.id = y.order_id', 'left')
                //->join('store_flow_copy sfc','sfc.order_id = y.id and sfc.pro_type in(1,2,5,12) and item_name not like "%商品%"','left')
                //->join('store_flow sf','sf.order_id = y.id and sf.pro_type in(1,2,5)','left')
                ->join('staff_flow_new sfn', 'sfn.order_id = y.id and sfn.pro_type in(1,2,3,5) and sfn.status!=3', 'left')
                ->where($where)
                ->field('o.id,o.type,o.ord_num,o.pay_money,o.balance_money,o.pay_time,o.ordtype,'
                        . 'sum(if(y.refund_status=2,y.money,0)) as tuiMoney,'
                        . 'if(sum(y.is_share)=0,"未分账",if(sum(y.is_share)<count(1),"部分分账","全部分账")) as share_status,'
                        //. 'sum(ifnull(sfc.money,0)) storeMoney,'
                        . 'sum(ifnull(sfn.money,0)) staffMoney')
                //. 'sum(ifnull(sf.money,0)) storeFlowMoney')
                ->group('y.order_id')
                ->select();
//        echo "<pre />";
//        print_r($orderList);die;

        foreach ($orderList as $k => $v) {
            if ($v['type'] == 1) {
                $item_name = '普通项目';
            } elseif ($v['type'] == 2) {
                $item_name = '超值组合';
            } elseif ($v['type'] == 5) {
                $item_name = '秒杀';
            }
            //实付金额
            $pay_money = $v['pay_money'] / 100;
            $balance_money = $v['balance_money'] / 100;
            $money = $pay_money + $balance_money;
            //退款金额
            $tuimoney = $v['tuiMoney'] / 100;
            //支付方式
            if ($v['ordtype'] == 1) {
                $ordtype = '小程序';
            } elseif ($v['ordtype'] == 2) {
                $ordtype = '余额';
            }
            //分账状态
            $is_share = $v['share_status'];
            //店铺入账
            $order_id = $v['id'];
            $yuyueids = Db::name('yuyue')->where('order_id', $order_id)->column('id');
            $storeMoney = Db::name('store_flow')
                    ->where('order_id', 'in', $yuyueids)
                    ->where('pro_type', 'in', '1,2,5')
                    ->sum('money');
            $storeMoney = $storeMoney / 100;
//            if($storeMoney == 0){
//                $storeMoney = $v['storeFlowMoney']/100;
//            }
            //美容师入账
            $staffMoney = $v['staffMoney'] / 100;
            //平台入账
            $kkmMoney = $money - $tuimoney-$storeMoney - $staffMoney;
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $item_name . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['ord_num'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $balance_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $tuimoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $ordtype . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $is_share . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $storeMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $staffMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $kkmMoney . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 无人店~医美订单明细
     * 日期 2021-05-27
     */

    public function yimeiOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=10 style='border:1px solid #999999;'>医美订单明细</td></tr>";
        echo "<tr align='center'><td colspan=10 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>项目名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "<td style='border:1px solid #999999;'>分账状态</td>"
        . "<td style='border:1px solid #999999;'>店铺入账</td>"
        . "<td style='border:1px solid #999999;'>合作商（线下）</td>"
        . "<td style='border:1px solid #999999;'>平台入账</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));
        
//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';

        $orderList = Db::name('order_yimei')
                ->alias('oy')
                ->join('item i', 'i.id = oy.item_id', 'left')
                ->where('oy.status', 1)
                ->where('oy.pay_time', 'between', [$startTime, $endTime])
                ->field('oy.id,oy.ord_num,oy.pay_money,oy.pay_time,oy.is_share,i.item_name')
                ->select();
//        echo "<pre />";
//        print_r($orderList);die;

        foreach ($orderList as $k => $v) {
            //实付金额
            $money = $v['pay_money'] / 100;

            //分账状态
            $is_share = $v['is_share'];
            if ($is_share == 1) {
                $is_share = '已分账';
            } else {
                $is_share = '未分账';
            }

            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['item_name'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['ord_num'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>小程序</td>";
            echo "<td style='border:1px solid #999999;'>" . $is_share . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $money * 0.45 . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $money * 0.45 . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $money * 0.1 . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 无人店~体验卡订单明细
     * 日期 2021-05-27
     */

    public function cardOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=11 style='border:1px solid #999999;'>体验卡订单明细</td></tr>";
        echo "<tr align='center'><td colspan=11 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>体验卡名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>零钱金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "<td style='border:1px solid #999999;'>分账状态</td>"
        . "<td style='border:1px solid #999999;'>店铺入账</td>"
        . "<td style='border:1px solid #999999;'>美容师</td>"
        . "<td style='border:1px solid #999999;'>平台入账</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));
        
//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';

        $orderList = Db::name('super_card_order')
                ->where('pay_status', 2)
                ->where('pay_time', 'between', [$startTime, $endTime])
                ->field('id,order_no,pay_money,balance_money,pay_time')
                ->select();
//        echo "<pre />";
//        print_r($orderList);die;

        foreach ($orderList as $k => $v) {
            //实付金额
            $pay_money = $v['pay_money'] / 100;
            $balance_money = $v['balance_money'] / 100;
            $money = $pay_money + $balance_money;
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>超值体验卡</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['order_no'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $balance_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>小程序</td>";
            echo "<td style='border:1px solid #999999;'>未分账</td>";
            echo "<td style='border:1px solid #999999;'></td>";
            echo "<td style='border:1px solid #999999;'></td>";
            echo "<td style='border:1px solid #999999;'></td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    /*
     * 无人店~商品订单明细
     * 日期 2021-05-14
     */

    public function xiaodianOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=16 style='border:1px solid #999999;'>商品订单明细</td></tr>";
        echo "<tr align='center'><td colspan=16 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>商品名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>零钱金额</td>"
        . "<td style='border:1px solid #999999;'>退款金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "<td style='border:1px solid #999999;'>分账状态</td>"
        . "<td style='border:1px solid #999999;'>厂家提现状态</td>"
        . "<td style='border:1px solid #999999;'>店铺入账</td>"
        . "<td style='border:1px solid #999999;'>美容师</td>"
        . "<td style='border:1px solid #999999;'>用户入账</td>"
        . "<td style='border:1px solid #999999;'>厂商入账</td>"
        . "<td style='border:1px solid #999999;'>平台入账</td>"
//        . "<td style='border:1px solid #999999;'>差</td>"
        . "<td style='border:1px solid #999999;'>备注</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));

//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';
        $list = Db::table('pay_order')
                ->where('pay_status', 1)
                ->where('pay_time', 'between', [$startTime, $endTime])
                ->field('id,order_id,pay_money,balance_money,pay_time,mini_shop_id')
                ->select();
        foreach ($list as $k => $v) {
            $order_id = $v['order_id'];

            $payOrderDetailIds = Db::table('pay_order_detail')
                    ->where('parent_order_id', $order_id)
                    ->column('id');
            $payOrderGoodsIds = Db::table('pay_order_goods')
                    ->where('pay_order_id','in',$payOrderDetailIds)
                    ->column('id');
            $tuiMoney = Db::table('pay_order_goods')
                    ->where('pay_order_id', 'in', $payOrderDetailIds)
                    ->where('is_refund', 2)
                    ->sum('price');
            //分账状态
            $fenzhangCount = Db::name('vender_flow')
                    ->where('pro_type', 'in', '7,9')
                    ->where('order_id', 'in', $payOrderDetailIds)
                    ->count();
            if ($fenzhangCount == 0) {
                $is_shart = '未分账'; //分账状态
            } elseif ($fenzhangCount == count($payOrderDetailIds)) {
                $is_shart = '已分账'; //分账状态
            } else {
                $is_shart = '部分分账'; //分账状态
            }
            //查是否已经提现
            $a = 0;
            for ($i = 0; $i < count($payOrderDetailIds); $i++) {
                $tixian = Db::name('vender_tixian')
                        ->where('find_in_set(:id,order_ids)', ['id' => $payOrderDetailIds[$i]])
                        ->find();
                if ($tixian) {
                    $a = $a + 1;
                }
            }

            if ($a == 0) {
                $tixinaStatus = '未提现';
            } elseif ($a == count($payOrderDetailIds)) {
                $tixinaStatus = '已提现';
            } else {
                $tixinaStatus = '部分提现';
            }
            //店铺分账
            $storeMoney = Db::name('store_flow')
                    ->where('pro_type', 'in', '7,11')
                    ->where('order_id', 'in', $payOrderDetailIds)
                    ->sum('money');
            //美容师分账
            $staffMoney = Db::name('staff_flow_new')
                    ->where('pro_type', 'in', '6,7')
                    ->where('order_id', 'in', $payOrderGoodsIds)
                    ->where('status', 2)
                    ->sum('money');
            //厂家入账
            $venderMoney = Db::name('vender_flow')
                    ->where('pro_type', 'in', '7,9')
                    ->where('order_id', 'in', $payOrderDetailIds)
                    ->sum('money');
            //kkm利润
            $kkmMoney = Db::name('kkm_flow')
                    ->where('order_id', 'in', $payOrderDetailIds)
                    ->where('pro_type', 'in', '7,9')
                    ->sum('money');
            //用户入账
            $ctmMoney = Db::name('ctm_flow')
                    ->where('pro_type', 9)
                    ->where('order_id', 'in', $payOrderDetailIds)
                    //->where('ctm_id',$v['mini_shop_id'])
                    ->where('ctm_id','in','select id from ai_custom where smallshop_type = 0')
                    ->sum('money');
            $pay_money = $v['pay_money'] / 100;
            $balance_money = $v['balance_money'] / 100;
            $tuiMoney = $tuiMoney / 100;
            $storeMoney = $storeMoney / 100;
            $staffMoney = $staffMoney / 100;
            $venderMoney = $venderMoney / 100;
            $kkmMoney = $kkmMoney / 100;
            $ctmMoney = $ctmMoney / 100;
            $q = bcadd($storeMoney,$staffMoney);
            $w = bcadd($venderMoney,$ctmMoney);
            $r = bcadd($q,$w);
            $e = bcadd($r,$kkmMoney);
            $cha = bcsub($pay_money,$e);
            //$kkmMoney = bcsub($pay_money,$r);
            if($is_shart !='未分账'){
                $kkmMoney = $pay_money+$balance_money-$tuiMoney-$storeMoney-$staffMoney-$ctmMoney-$venderMoney;
                $note = '';
            }  else {
                $note = '没有分账，无法确定具体分账数值';
            }
            

            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>小店商品</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['order_id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $balance_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $tuiMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . '小程序' . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $is_shart . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $tixinaStatus . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $storeMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $staffMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $ctmMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $venderMoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $kkmMoney . "</td>";
//            echo "<td style='border:1px solid #999999;'>" . $cha . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $note . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    /*
     * 无人店~特权订单明细
     * 日期 2021-06-08
     */

    public function specialOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=11 style='border:1px solid #999999;'>特权订单明细</td></tr>";
        echo "<tr align='center'><td colspan=11 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>项目名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>零钱金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "<td style='border:1px solid #999999;'>店铺入账</td>"
        . "<td style='border:1px solid #999999;'>美容师入账</td>"
        . "<td style='border:1px solid #999999;'>平台入账</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));
        
//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';

        $orderList = Db::name('order')
                ->where('type', 4)
                ->where('status',2)
                ->where('pay_time', 'between', [$startTime, $endTime])
                ->field('id,ord_num,pay_money,balance_money,pay_time,ordtype')
                ->select();
//        echo "<pre />";
//        print_r($orderList);die;

        foreach ($orderList as $k => $v) {
            //实付金额
            $money = ($v['pay_money']+$v['balance_money']) / 100;
            $pay_money = $v['pay_money']/100;
            $balance_money = $v['balance_money']/100;
            $storemoney = Db::name('store_flow')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->value('money');
            $storemoney = $storemoney/100;
            $staffmoney = Db::name('staff_flow_new')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->where('status',2)
                    ->value('money');
            $staffmoney = $staffmoney/100;
            $kkmmoney = Db::name('kkm_flow')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->value('money');
            $kkmmoney = $kkmmoney/100;
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>99特权卡</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['ord_num'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $balance_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>小程序</td>";
            echo "<td style='border:1px solid #999999;'>" . $storemoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $staffmoney . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $kkmmoney . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    /*
     * 无人店~充值订单明细
     * 日期 2021-06-10
     */

    public function topUpOrderList() {
        echo "<table style='border-collapse:collapse;'>";
        echo "<tr align='center'><td colspan=8 style='border:1px solid #999999;'>充值订单明细</td></tr>";
        echo "<tr align='center'><td colspan=8 style='border:1px solid #999999;'>导出时间：" . date('Y-m-d H:i:s') . "</td></tr>";
        echo "<tr>"
        . "<td style='border:1px solid #999999;'>编号</td>"
        . "<td style='border:1px solid #999999;'>项目名称</td>"
        . "<td style='border:1px solid #999999;'>订单编号</td>"
        . "<td style='border:1px solid #999999;'>实付金额</td>"
        . "<td style='border:1px solid #999999;'>零钱金额</td>"
        . "<td style='border:1px solid #999999;'>支付时间</td>"
        . "<td style='border:1px solid #999999;'>支付方式</td>"
        . "</tr>";
        $year = input('param.y');
        $smonth = input('param.s');
        $emonth = input('param.e');
        $startTime = $year . '-' . $smonth . '-1 00:00:00';
        $em = $year . '-' . $emonth . '-31 23:59:59';
        $endTime = date('Y-m-d H:i:s', strtotime($em));
        
//        $startTime = '2021-06-01 00:00:00';
//        $endTime = '2021-06-30 23:59:59';

        $orderList = Db::name('order')
                ->where('type', 3)
                ->where('status',2)
                ->where('pay_time', 'between', [$startTime, $endTime])
                ->field('id,ord_num,pay_money,balance_money,pay_time,ordtype')
                ->select();
//        echo "<pre />";
//        print_r($orderList);die;

        foreach ($orderList as $k => $v) {
            //实付金额
            $money = ($v['pay_money']+$v['balance_money']) / 100;
            $pay_money = $v['pay_money']/100;
            $balance_money = $v['balance_money']/100;
            $storemoney = Db::name('store_flow')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->value('money');
            $storemoney = $storemoney/100;
            $staffmoney = Db::name('staff_flow_new')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->where('status',2)
                    ->value('money');
            $staffmoney = $staffmoney/100;
            $kkmmoney = Db::name('kkm_flow')
                    ->where('order_id',$v['id'])
                    ->where('pro_type',4)
                    ->value('money');
            $kkmmoney = $kkmmoney/100;
            echo "<tr>";
            echo "<td style='border:1px solid #999999;'>" . $v['id'] . "</td>";
            echo "<td style='border:1px solid #999999;'>充值</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['ord_num'] . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $pay_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $balance_money . "</td>";
            echo "<td style='border:1px solid #999999;'>" . $v['pay_time'] . "</td>";
            echo "<td style='border:1px solid #999999;'>小程序</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

}
